﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LPV_POS.Module;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using System.Resources;
using System.Globalization;
using LPV_POS.thuvien_cls;

namespace LPV_POS.mh_pos
{

    public partial class mh_dmkhohang : DevExpress.XtraEditors.XtraForm
    {
        CultureInfo culture;
        string str_lang = "";
        thongbao_lang cls_thongbao = new thongbao_lang();
        thuvien tv;
        DataTable tb;
        string[] s;
        object[] o;

        #region "Muti language"

        private void SetLanguage_banhang(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_banhang6_PhuThu).Assembly);
                label3.Text = rm.GetString("ten", culture);
                label1.Text = rm.GetString("dienthoai", culture);
                label4.Text = rm.GetString("fax", culture);
                label5.Text = rm.GetString("diachi", culture);
                label6.Text = rm.GetString("ghichu", culture);
                bton_clr.Text = rm.GetString("clr", culture);
                bton_them.Text = rm.GetString("them", culture);
                bton_sua.Text = rm.GetString("sua", culture);
                bton_xoa.Text = rm.GetString("xoa", culture);
                bton_luu.Text = rm.GetString("luu", culture);
                bton_thoat.Text = rm.GetString("thoat", culture);
                col_diachi.Caption = rm.GetString("diachi", culture);
                col_fax.Caption = rm.GetString("fax", culture);
                col_ghichu.Caption = rm.GetString("ghichu", culture);
                //col_makho.Caption = rm.GetString("khongvat", culture);
                //col_nguoilienhe.Caption = rm.GetString("vat", culture);
                col_sodt.Caption = rm.GetString("dienthoai", culture);
                col_tenkho.Caption = rm.GetString("ten", culture);
                this.grid_khohang.GroupPanelText = rm.GetString("danhsachkhohang", culture);
                 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion

        // select danh sach kho
        private DataTable f_selectdmkho()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("sp_mh_dmkhohang", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }

        // insert them mot kho hang
        private int f_insertkhohang(string tenkhohang, string sodienthoai, string fax, string diachi, string ghichu)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME", "@DIACHI", "@DIENTHOAI", "@FAX", "@GHICHU" };
            o = new object[] { 2, 1, tenkhohang, diachi, sodienthoai, fax, ghichu };
            int ketqua = tv.SP_Update("sp_mh_dmkhohang", s, o);
            return ketqua;
        }
        // update mot kho hang
        private int f_updatekhohang(string makhohang,string tenkhohang, string sodienthoai, string fax, string diachi, string ghichu)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID", "@OBJNAME", "@DIACHI", "@DIENTHOAI", "@FAX", "@GHICHU" };
            o = new object[] { 3, 1, makhohang,tenkhohang, diachi,sodienthoai, fax,  ghichu};
            int ketqua = tv.SP_Update("sp_mh_dmkhohang", s, o);
            return ketqua;
        }
        // xoa kho hang
        private int f_deletethueVAT(string makhohang)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 4, 1, makhohang };
            int ketqua = tv.SP_Update("sp_mh_dmkhohang", s, o);
            return ketqua;
        }

        // Tim kho khi nhan nut tim
        private DataTable f_selectfindkhohang(string tenkhohang, string sodienthoai, string fax, string diachi, string ghichu)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME", "@DIACHI", "@DIENTHOAI", "@FAX", "@GHICHU" };
            o = new object[] { 1, 2, tenkhohang, diachi,sodienthoai, fax,  ghichu };
            tb = tv.SP_SelectDataTable("sp_mh_dmkhohang", s, o);
            return tb;
        }

        // tim thue VAT
        private DataTable f_findkhohang()
        {
            DataTable tb;
            string tenkhohang;
            string sodienthoai;
            string fax;
            string diachi;
            string ghichu;
            if (txt_tenkho.Text != "")
                tenkhohang = "'%" + txt_tenkho.Text + "%'";
            else
                tenkhohang = "";

            if (txt_sodienthoai.Text != "")
                sodienthoai = "'%" + txt_sodienthoai.Text + "%'";
            else
                sodienthoai = txt_sodienthoai.Text;

            if (txt_fax.Text != "")
                fax = "'%" + txt_fax.Text + "%'";
            else
                fax = txt_fax.Text ;

            if (txt_diachi.Text != "")
                diachi = "'%" + txt_diachi.Text + "%'";
            else
                diachi = txt_diachi.Text;

            if (txt_ghichu.Text != "")
                ghichu = "'%" + txt_ghichu.Text + "%'";
            else
                ghichu = txt_ghichu.Text;


            tb = f_selectfindkhohang(tenkhohang,sodienthoai,fax,diachi,ghichu);
            return tb;
        }

        public mh_dmkhohang()
        {
            InitializeComponent();
            culture = CultureInfo.CurrentCulture;
        }

        private void mh_dmkhohang_Load(object sender, EventArgs e)
        {
            cpublic ocpublic = new cpublic();
            str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_banhang(str_lang);


            luoi_khohang.MainView = grid_khohang;
            luoi_khohang.DataSource = null;
            luoi_khohang.DataSource = f_selectdmkho();
           // bton_tim.Text = "CLR";
        }

        private void bton_thoat_Click(object sender, EventArgs e)
        {
            //Mod_messagebox mess = new Mod_messagebox();
            //mess.messagebox_pos("Thông báo", "Bạn muốn thoát?", loaithongbao.yesno.GetHashCode());
            cls_thongbao = new thongbao_lang();
            cls_thongbao.show_info(str_lang, "chatchan");
            if (Variable.thongbao == true)
            {
                this.Close();
            }
        }

        private void grid_khohang_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            GridView grid = (GridView)sender;
            int chiso = e.FocusedRowHandle;
            string _chiso = Convert.ToString(chiso);
            GridColumn col = grid_khohang.FocusedColumn;
            txt_tenkho.Text = grid_khohang.GetRowCellDisplayText(chiso, col_tenkho) + "";
            txt_sodienthoai.Text = grid_khohang.GetRowCellDisplayText(chiso, col_sodt) + "";
            txt_fax.Text = grid_khohang.GetRowCellDisplayText(chiso, col_fax) + "";
            txt_diachi.Text = grid_khohang.GetRowCellDisplayText(chiso, col_diachi) + "";
            //txt_nguoilienhe.Text = grid_khohang.GetRowCellDisplayText(chiso, col_nguoilienhe) + "";
            txt_ghichu.Text = grid_khohang.GetRowCellDisplayText(chiso, col_ghichu) + "";
           
            bton_tenkho.Enabled = false;
            bton_sodienthoai.Enabled = false;
            bton_fax.Enabled = false;
            bton_diachi.Enabled = false;
            bton_ghichu.Enabled = false;
            bton_luu.Enabled = false;
        }

        private void bton_tenkho_Click(object sender, EventArgs e)
        {
            //mh_chu frm_chu = new mh_chu();
            //frm_chu.ControlBox = false;

            //Variable.str_position_frmchu = "mhdmkhohang_bton_tenkho";
            //frm_chu.ShowDialog();
            //txt_tenkho.Text = Variable.str_chu;
            txt_tenkho.Text = new cpublic().loadformchu_point(140, 280);

        }

        private void bton_sodienthoai_Click(object sender, EventArgs e)
        {
            //mh_so frm_so = new mh_so();
            //frm_so.ControlBox = false;
            //Variable.str_position_frmso = "mhdmkhohang_slcanquydoi";
            //frm_so.ShowDialog();
            //txt_sodienthoai.Text = Variable.str_so;
            txt_sodienthoai.Text = new cpublic().loadformso_point(670, 270);
        }

        private void bton_fax_Click(object sender, EventArgs e)
        {
            //mh_so frm_so = new mh_so();
            //frm_so.ControlBox = false;
            //Variable.str_position_frmso = "mhdmkhohang_slcanquydoi";
            //frm_so.ShowDialog();
            //txt_fax.Text = Variable.str_so;
            txt_fax.Text = new cpublic().loadformso_point(670, 270);
        }

        private void bton_diachi_Click(object sender, EventArgs e)
        {
            //mh_chu frm_chu = new mh_chu();
            //frm_chu.ControlBox = false;
            //Variable.str_position_frmchu = "mhdmkhohang_bton_tenkho";
            //frm_chu.ShowDialog();
            //txt_diachi.Text = Variable.str_chu;
            txt_diachi.Text = new cpublic().loadformchu_point(140, 280);
        }

        private void bton_ghichu_Click(object sender, EventArgs e)
        {
            //mh_chu frm_chu = new mh_chu();
            //frm_chu.ControlBox = false;
            //Variable.str_position_frmchu = "mhdmkhohang_bton_tenkho";
            //frm_chu.ShowDialog();
            //txt_ghichu.Text = Variable.str_chu;
            txt_ghichu.Text = new cpublic().loadformchu_point(140, 280);
        }

        private void bton_them_Click(object sender, EventArgs e)
        {
            txt_tenkho.Text = "";
            txt_sodienthoai.Text = "";
            txt_fax.Text = "";
            txt_diachi.Text = "";
            txt_ghichu.Text = "";
            bton_luu.Enabled = true;
            bton_tenkho.Enabled = true;
            bton_sodienthoai.Enabled = true;
            bton_fax.Enabled = true;
            bton_diachi.Enabled = true;
            bton_ghichu.Enabled = true;
            Variable.str_trangthaithemsua = "them";
        }

        private void bton_sua_Click(object sender, EventArgs e)
        {
            bton_luu.Enabled = true;
            bton_luu.Enabled = true;
            bton_tenkho.Enabled = true;
            bton_sodienthoai.Enabled = true;
            bton_fax.Enabled = true;
            bton_diachi.Enabled = true;
            bton_ghichu.Enabled = true;
            Variable.str_trangthaithemsua = "sua";
        }

        private void bton_xoa_Click(object sender, EventArgs e)
        {
            cls_thongbao = new thongbao_lang();
            int ketqua;
            if (luoi_khohang.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;
            string makhohang = grid_khohang.GetRowCellValue(grid_khohang.FocusedRowHandle, col_makho).ToString();
            //Mod_messagebox mess = new Mod_messagebox();
            //mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
            cls_thongbao.show_info(str_lang, "chatchan");
            if (Variable.thongbao == true)
            {
                ketqua = f_deletethueVAT(makhohang);
                if (ketqua == 2)
                {
                   
                    //mess.messagebox_pos("Thông báo", "Xóa không thành công", loaithongbao.canhbao.GetHashCode());
                    cls_thongbao.show_info(str_lang, "khongthanhcong");
                    return;
                }
                // xoa mot dong trong luoi don vi tinh
                int chiso;
                chiso = ((ColumnView)luoi_khohang.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)luoi_khohang.Views[0].DataSource;
                tam.Delete(chiso);
            }
        }

        private void bton_luu_Click(object sender, EventArgs e)
        {
            int ketqua;
            cls_thongbao = new thongbao_lang();
            if (txt_tenkho.Text == "")       // không thể luu neu chua nhap ten don vi tinh
                return;
            int chiso = grid_khohang.FocusedRowHandle;
            if (Variable.str_trangthaithemsua == "them")
            {
                ketqua = f_insertkhohang(txt_tenkho.Text, txt_sodienthoai.Text , txt_fax.Text, txt_diachi.Text, txt_ghichu.Text);

                if (ketqua == -1)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    //mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());
                    cls_thongbao.show_info(str_lang, "khongthanhcong");
                    return;
                }
            }
            else if (Variable.str_trangthaithemsua == "sua")
            {
                string makhohang = grid_khohang.GetRowCellValue(grid_khohang.FocusedRowHandle, col_makho).ToString();
                ketqua = f_updatekhohang(makhohang,txt_tenkho.Text, txt_sodienthoai.Text, txt_fax.Text, txt_diachi.Text, txt_ghichu.Text );

                if (ketqua == -1)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    //mess.messagebox_pos("Thông báo", "Chỉnh sửa không thành công", loaithongbao.canhbao.GetHashCode());
                    cls_thongbao.show_info(str_lang, "khongthanhcong");
                    return;
                }
                // luoi_nhomchuhh.DataSource = f_selectdsnhomchuhh();
            }
          
            luoi_khohang.DataSource = null;
            luoi_khohang.DataSource = f_selectdmkho();
            if (Variable.str_trangthaithemsua == "sua")
                grid_khohang.FocusedRowHandle = chiso;
            //else if (Variable.str_trangthaithemsua == "them")
            //    grid_khohang.FocusedRowHandle = grid_khohang.RowCount - 1;

        }

        private void bton_tim_Click(object sender, EventArgs e)
        {
            //if (bton_tim.Text == "CLR")
            //{
            //    txt_tenkho.Text = "";
            //    txt_sodienthoai.Text = "";
            //    txt_fax.Text = "";
            //    txt_diachi.Text = "";
            //    txt_ghichu.Text = "";
            //    bton_tim.Text = "Tìm";
            //}
            //else
            //{
                luoi_khohang.DataSource = null;
                luoi_khohang.DataSource = f_findkhohang();
              //  bton_tim.Text = "CLR";
            //}
        }

        private void bton_clr_Click(object sender, EventArgs e)
        {
            txt_tenkho.Text = "";
            txt_sodienthoai.Text = "";
            txt_fax.Text = "";
            txt_diachi.Text = "";
            txt_ghichu.Text = "";
            bton_luu.Enabled = true;
            bton_luu.Enabled = true;
            bton_tenkho.Enabled = true;
            bton_sodienthoai.Enabled = true;
            bton_fax.Enabled = true;
            bton_diachi.Enabled = true;
            bton_ghichu.Enabled = true;
            //bton_tim.Text = "Tìm";

        }
    }
}